package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/config/global"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/common/services"
)

// @Summary 获取滑块验证码图片
// @Description 获取滑块验证码图片
// @Accept  json
// @Produce  json
// @Tags Captcha
// @Success 200 {object} api_models.CaptchaResp "result"
// @Router /api/captcha [POST]
func (base Controller) Captcha(c echo.Context) error {
	imgPath := "../../../pkg/common/helper/target/"
	resp := services.GetCaptchaImage(imgPath)
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取滑块验证码图片原图地址
// @Description 获取滑块验证码图片原图地址
// @Accept  json
// @Produce  json
// @Tags Captcha
// @Success 200 {object} api_models.CaptchaResp "result"
// @Router /api/captcha_path [POST]
func (base Controller) CaptchaPath(c echo.Context) error {
	imgPath := "../../../pkg/common/helper/target/"
	resp, err := services.GetCaptchaImagePath(imgPath)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp.Image = global.Config.GetString("captcha_path.url") + resp.Image
	resp.Slide = global.Config.GetString("captcha_path.url") + resp.Slide
	global.Log.Debugf("CaptchaPath ip:%v,resp:%+v", c.RealIP(), resp)
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取滑块验证码图片
// @Description 获取滑块验证码图片
// @Accept  json
// @Produce  json
// @Tags Captcha
// @Success 200 {object} api_models.CaptchaVerifyReq "result"
// @Router /api/captcha_verify [POST]
func (base Controller) CaptchaVerify(c echo.Context) error {
	r := &api_models.CaptchaVerifyReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	resp := services.Check(r.ID, r.X)
	return util.BuildSuccess(c, resp, "")
}
